#include "Helper.h"
#include <QDir>

QString Helper::fixFileName(const QString filePath){
    QFileInfo fileInfo(filePath);

    const QString path = fileInfo.path();
    const QString name = fileInfo.fileName();
    const QString fixName = Helper::_fixName(name);

    return QDir(path).filePath(fixName);
}

QString Helper::_fixName(const QString fileName){
    // TODO 后续将所有特殊字符进行替换
    size_t lastIndex = fileName.lastIndexOf('.');
    QString leftStr = fileName.left(lastIndex);
    QString rightStr = fileName.right(fileName.length() - lastIndex);

    // TODO 后续将所有特殊字符进行替换
    leftStr = leftStr.replace('.', '_');
    leftStr = leftStr.replace('-', '_');

    return leftStr + rightStr;
}

QString Helper::unicode16to8(QString unicode){
    QString res;
    size_t startLoc = 0;
    size_t u16Idx = -1;

    while(true){
        u16Idx = unicode.indexOf("\\\\u");
        if(u16Idx == -1){
            res.append(unicode.right(unicode.length() - startLoc));
            break;
        }

        if(u16Idx != startLoc) res.append(unicode.mid(startLoc, u16Idx - startLoc));
        QString s1 = unicode.mid(u16Idx + 3, 4);
        res.append(s1.toUShort(0, 16));

        unicode.remove(u16Idx, 7);
        startLoc = u16Idx;
    }
    return res;
}
